package com.hake.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hake.model.PetOwnerRegistBean;
import com.hake.service.PetUserServiceImpl;
import com.hake.util.ErrorInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2017/4/1.
 */

@RestController
@RequestMapping("/pet/user")
public class PetUserController {

    private static Logger logger = LoggerFactory.getLogger(PetUserController.class);

    @Autowired
    private PetUserServiceImpl petUserService;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public ModelAndView hello() {
        ModelAndView mv = new ModelAndView("hello");
        return mv;
    }

    @RequestMapping(value = "/test")
    public String setupForm(@RequestParam("userkey") String a) {

        System.out.println("test :"+a);
        System.out.println();
        return "hello";
    }

    @RequestMapping(value = "/add")
    public String df(@RequestParam ("petOwnerRegistBean") String json) {

        ObjectMapper mapper = new ObjectMapper();
        PetOwnerRegistBean petOwnerRegistBean = null;
        try {
            petOwnerRegistBean = mapper.readValue(json, PetOwnerRegistBean.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(petOwnerRegistBean.toString());

        String user_id = petOwnerRegistBean.getUser_id();
        String pet_owner_name = petOwnerRegistBean.getPet_owner_name();
        String pet_owner_phone = petOwnerRegistBean.getPet_owner_phone();
        String pet_owner_qq = petOwnerRegistBean.getPet_owner_qq();
        String pet_owner_wx = petOwnerRegistBean.getPet_owner_wx();
        String pet_owner_card_type = petOwnerRegistBean.getPet_owner_card_type();
        String pet_owner_card_number = petOwnerRegistBean.getPet_owner_card_number();
        String pet_owner_place = petOwnerRegistBean.getPet_owner_place();
        String pet_owner_street = petOwnerRegistBean.getPet_owner_street();
        String pet_owner_photo = petOwnerRegistBean.getPet_owner_photo();
        String pet_name = petOwnerRegistBean.getPet_name();
        String pet_type = petOwnerRegistBean.getPet_type();
        String pet_sex = petOwnerRegistBean.getPet_sex();
        String pet_birth = petOwnerRegistBean.getPet_birth();
        String pet_immune_no = petOwnerRegistBean.getPet_immune_no();
        String pet_immune_group = petOwnerRegistBean.getPet_immune_group();
        String pet_immune_able_date = petOwnerRegistBean.getPet_immune_able_date();
        String pet_birthday = petOwnerRegistBean.getPet_birthday();
        String pet_id_no = petOwnerRegistBean.getPet_id_no();
        String pet_police_station = petOwnerRegistBean.getPet_police_station();
        String pet_photo_positive = petOwnerRegistBean.getPet_photo_positive();
        String pet_photo_side = petOwnerRegistBean.getPet_photo_side();

        int result = petUserService.add(user_id,pet_owner_name,pet_owner_phone,pet_owner_qq,pet_owner_wx,pet_owner_card_type,pet_owner_card_number,pet_owner_place,pet_owner_street,pet_owner_photo,pet_name,pet_type,pet_sex,pet_birth,pet_immune_no,pet_immune_group,pet_immune_able_date,pet_birthday,pet_id_no,pet_police_station,pet_photo_positive, pet_photo_side);

        ErrorInfo errorInfo = new ErrorInfo();

        if(result >= 1) {
            errorInfo.setErrInfo("0", "success");
        }else{
            errorInfo.setErrInfo(String.valueOf(result), "failed");
        }
        return JSON.toJSONString(errorInfo);
    }
}
